package fr.unice.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.util.ServiceException;

import fr.unice.outils.Results_videos;

public class Search_videos extends HttpServlet{
        public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

                String keywords = null;
                keywords = req.getParameter( "keywords" );
                String order =null;
                order = req.getParameter( "order" );
                String category = null;
                category = req.getParameter( "category" );
                String adultContent= null;
                adultContent = req.getParameter( "adultContent" );

                String startIndex;
                startIndex = req.getParameter( "startIndex" );
                String pageCour;
                pageCour = req.getParameter( "pageCour" );

                String standardFeed=null;
                standardFeed = req.getParameter( "standardFeed" );


        // setup the response
        res.setContentType("text/xml;charset=UTF-8");
        res.setHeader("Cache-Control", "no-cache");
        // write out the response string

        //verif que le serveur repond bien
        //res.getWriter().write("Serveur OK!");

                YouTubeService myService = new YouTubeService("gdataSample-YouTube-2");

                if (keywords != null) {
                        //Searches the VIDEO_FEED for user supplied search terms.
                        try{
                        String results = Results_videos.searchFeed(myService, keywords, order, category, adultContent, startIndex, pageCour);
                        res.getWriter().write(results);
                        }
                        catch (IOException e) {
                                // Communications error
                                System.err.println("There was a problem communicating with the service.");
                                e.printStackTrace();
                        }catch (ServiceException e) {
                                // Server side error
                                System.err.println("The server had a problem handling your request.");
                                e.printStackTrace();
                        }
        }
                //si requete sur les standards feeds
                else if(standardFeed!=null){
                        //Searches the STANDARD_FEED.
                        try{
                        String results = Results_videos.searchStandardFeed(myService, standardFeed, startIndex, pageCour);
                        res.getWriter().write(results);
                        }
                        catch (IOException e) {
                                // Communications error
                                System.err.println("There was a problem communicating with the service.");
                                e.printStackTrace();
                        }catch (ServiceException e) {
                                // Server side error
                                System.err.println("The server had a problem handling your request.");
                                e.printStackTrace();
                        }

                }


        else {
            // If keywords comes back as a null, return a question mark.
            res.setContentType("text/xml;charset=UTF-8");
            res.setHeader("Cache-Control", "no-cache");
            res.getWriter().write("KO!!!");
        }
        }

}
